from django.db import models
from django.contrib.auth.models import User

from tagging.fields import TagField
from tagging.models import Tag

class Organisation(models.Model):
  name = models.CharField(max_length=200)
  description = models.TextField(blank = True, null = True)
  url = models.URLField(blank = True, null = True)
  parent = models.ForeignKey('self', blank=True, null=True, related_name='subGroup')
  tags = TagField(null=True)
  def __unicode__(self):
    return self.name

class Client(models.Model):
  user = models.ForeignKey(User, related_name="user_account")
  owner = models.ForeignKey(User, related_name="client_owner")
  office_phone = models.CharField(max_length = 20,  blank = True, null = True);
  mobile_phone = models.CharField(max_length = 20,  blank = True, null = True);
  home_phone = models.CharField(max_length = 20,  blank = True, null = True);
  url = models.URLField(blank = True, null = True)
  tags = TagField(null=True)
  roles = models.ManyToManyField(Organisation, through='Role', null = True)
  def __unicode__(self):
    return self.user.first_name + " " + self.user.last_name

class Role(models.Model):
  title = models.CharField(max_length=100)
  description = models.TextField()
  organisation = models.ForeignKey(Organisation)  
  client = models.ForeignKey(Client)
  def __unicode__(self):
    return self.title + ", " + self.organisation.name
  
class RemoteResource(models.Model):
  title = models.CharField(max_length=140)
  url = models.URLField(verify_exists=True)
  client = models.ForeignKey(Client, blank=True, null=True)
  organisation = models.ForeignKey(Organisation, blank=True, null = True)
  def __unicode__(self):
    return self.title

class Communication(models.Model):
  subject = models.CharField(max_length=140)
  source = models.ForeignKey(Client, related_name="from")
  recipient = models.ManyToManyField(Client, related_name="to")
  content = models.TextField()
  sent = models.BooleanField(default = False)
  def __unicode__(self):
    return self.subject + " (from " + self.source.user.first_name + " " + self.source.user.last_name + ")"


  
